<?php

namespace app\admin\model\repertory;

use app\common\model\repertory\Bill as Models;
use app\common\model\repertory\BillDetail;
use app\common\model\repertory\BillSet;
use think\facade\Db;

class Bill extends Models
{
    /**
     * 获取全部
     */
    public static function getAll($data, $deptId)
    {
        $model = new static;
        $where = [];
        if(isset($data["type"]) && !empty($data["type"])) {
            $where[] = ['type', '=', $data["type"]];
        }
        $where[] = ['dept_id', '=', $deptId];
        return $model->where('is_delete', '=', 0)->where($where)->select();
    }

    /**
     * 新增
     */
    public function add($data, $deptId)
    {
        $billDetailData = $data["details"];
        $billSetData = $data["sets"];
        unset($data["details"]);
        unset($data["sets"]);
        $data["dept_id"] = $deptId;
        $data["create_time"] = time();
        $data["update_time"] = time();
        $bill_id = $this->insertGetId($data);
        if ($bill_id && $bill_id > 0) {
            $billSetData["bill_id"] = $bill_id;
            $billSetData["dept_id"] = $deptId;
            $billSetData["create_time"] = time();
            $billSetData["update_time"] = time();
            
            $billSetData["flag"] = $billSetData["flag"] == 'true' ? 1 : 2;
            $billSetData["print"] = $billSetData["print"] == 'true' ? 1 : 2;
            $billSetData["bill_date"] = $billSetData["bill_date"] == 'true' ? 1 : 2;
            $billSetData["pay_date"] = $billSetData["pay_date"] == 'true' ? 1 : 2;
            $billSetData["units"] = $billSetData["units"] == 'true' ? 1 : 2;
            $billSetData["delivery_date"] = $billSetData["delivery_date"] == 'true' ? 1 : 2;
            $billSetData["delivery_no"] = $billSetData["delivery_no"] == 'true' ? 1 : 2;
            $billSetData["tax0"] = $billSetData["tax0"] == 'true' ? 1 : 2;
            BillSet::insert($billSetData);
            foreach ($billDetailData as &$val) {
                if (!is_numeric($val["product_id"])) {
                    $proDatas = [
                        "store_id" => $deptId,
                        "class_id" => 0,
                        "title" => $val["title"],
                        "price" => $val["price"],
                        "create_time" => time(),
                        "update_time" => time(),
                        "dept_id" => $deptId,
                    ];
                    $proId = Product::insertGetId($proDatas);
                    $val["product_id"] = $proId;
                }
                $val["bill_id"] = $bill_id;
                $val["create_time"] = time();
                $val["update_time"] = time();
            }
            unset($val);
            BillDetail::insertAll($billDetailData);
            return true;
        } else {
            return false;
        }
    }


    /**
     * 软删除
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 更新记录
     */
    public function edit($data)
    {
        $res = $this->save($data);
        if ($res) {
            $billDetailData = $data["details"];
            $billSetData = $data["sets"];
            unset($data["details"]);
            unset($data["sets"]);
            $billSetData["update_time"] = time();

            $billSetData["flag"] = $billSetData["flag"] == 'true' ? 1 : 2;
            $billSetData["print"] = $billSetData["print"] == 'true' ? 1 : 2;
            $billSetData["bill_date"] = $billSetData["bill_date"] == 'true' ? 1 : 2;
            $billSetData["pay_date"] = $billSetData["pay_date"] == 'true' ? 1 : 2;
            $billSetData["units"] = $billSetData["units"] == 'true' ? 1 : 2;
            $billSetData["delivery_date"] = $billSetData["delivery_date"] == 'true' ? 1 : 2;
            $billSetData["delivery_no"] = $billSetData["delivery_no"] == 'true' ? 1 : 2;
            $billSetData["tax0"] = $billSetData["tax0"] == 'true' ? 1 : 2;
            BillSet::where(['id' => $billSetData["id"]])->update($billSetData);
            Db::name("repertory_bill_detail")->where(["bill_id" => $data["id"]])->delete();
            $BillDetailDatas = [];
            foreach ($billDetailData as &$val) {
                if (!is_numeric($val["product_id"])) {
                    $proDatas = [
                        "store_id" => $data["dept_id"],
                        "class_id" => 0,
                        "title" => $val["title"],
                        "price" => $val["price"],
                        "create_time" => time(),
                        "update_time" => time(),
                        "dept_id" => $data["dept_id"],
                    ];
                    $proId = Product::insertGetId($proDatas);
                    $val["product_id"] = $proId;
                }
                $val["bill_id"] = $data["id"];
                $val["create_time"] = time();
                $val["update_time"] = time();
                $BillDetailDatas[] = [
                    "delivery_date" => $val["delivery_date"],
                    "product_id" => $val["product_id"],
                    "title" => $val["title"],
                    "price" => $val["price"],
                    "year" => $val["year"],
                    "nums" => $val["nums"],
                    "tax" => $val["tax"],
                    "tax_title" => $val["tax_title"],
                    "delivery_no" => $val["delivery_no"],
                    "content" => $val["content"],
                    "rate_money" => $val["rate_money"],
                    "money" => $val["money"],
                    "bill_id" => $val["bill_id"],
                    "create_time" => $val["create_time"],
                    "update_time" => $val["update_time"]
                ];
            }
            unset($val);
            BillDetail::insertAll($BillDetailDatas);
            return true;
        } else {
            return false;
        }
    }

}